<?php
namespace admin;

use models\BaseDoo;

class Client extends Admin{
    protected $dataBase;
    public function __construct()
    {
        $this->assign('lh_php','LH_PHP');
        $this->dataBase = new BaseDoo();
        parent::__construct();
    }

    //客户列表
    public function index(){
        $this->assign('title','客户列表');
        //查找客户列表
        //所有的关键字都必须大写
        $field = ['id','client_name','client_abbre','client_status','address','client_code','logo','username','create_time','order_open_close'];
        $client_list = $this->dataBase->select('client',$field,['ORDER'=>['order_open_close'=>'DESC'],'LIMIT'=>50]);
        $this->assign('client_list',$client_list);
        $this->display('client/index');
    }

    //添加客户
    public function add(){
        if(isset($_POST['client_submit'])){
            unset($_POST['client_submit']);
            $_POST['password'] = sha1($_POST['password']);
            $this->dataBase->insert('client',$_POST);
            if( $this->dataBase->id()){
                $this->success('/admin/client','添加成功');
            }else{
                $this->error('/admin/client','添加失败');
            }
        }
        $this->display('client/add');
    }

    //修改客户
    public function edit(int $id){
        $find = $this->dataBase->get('client','*',['id'=>$id]);
        $this->assign($find);
        $this->display('client/edit');
    }


    //正式进行修改
    public function update(){
        $id = $_POST['id'];
        unset($_POST['id']);
        $update = $this->dataBase->update('client',$_POST,['id'=>$id]);
        if($update){
            $this->success('/admin/client','修改成功');
        }else{
            $this->error('/admin/client','修改失败');
        }
    }

    //进行删除操作
    public function delete($id)
    {
        if ($this->dataBase->delete('client', ['id' => $id])) {
            return show(1,'删除成功',[]);
        } else {
            return show(0,'删除失败',[]);
        }
    }


    //进行排序操作
    public function order(){
        $num = 0;
        foreach ($_POST['orderS'] as $id => $ord){
            $num += $this->dataBase->update('client',['order_open_close'=>$ord],['id'=>$id]);
        }
        if($num > 0){
            $this->success('/admin/client','修改成功');
        }else{
            $this->error('/admin/client','修改失败');
        }
    }




}